/*
 * @package	educME
 * @copyright (c) Jose Raul Perez 2011 under GPL
 */

function moodle_application(checknum, pid, args) {
    var app = new eyeos.application.Moodle(checknum, pid);
    app._initWithSplashScreen();
}

qx.Class.define("eyeos.application.Moodle", {
    extend: eyeos.system.EyeApplication,
    construct: function(checknum, pid) {
        arguments.callee.base.call(this,"Moodle", checknum, pid);
        this._checknum = checknum;
    },

    members: {
        _win: null,
        _iframe:null,
        
        _initWithSplashScreen: function() {
/*            var splash = new qx.ui.basic.Image("index.php?extern=/images/moodle/theme/moodle-splash.png").set({
                zIndex: 100002
            });
            var dimensions = document.eyeDesktop.getBounds();
            if (dimensions != null) {
                document.eyeDesktop.add(splash, {
                    left: (dimensions.width / 2) - 100,
                    top: (dimensions.height / 2) - 75
                });
            }
*/
            // would be nice a splash while login
            eyeos.callMessage(this._checknum, 'login', null, function (user) {

                // this, will create the cookie for iframe web interface
		var req = new qx.io.remote.Request( host() + "/moodle/login/index.php", "POST", "text/plain");
		req.setParameter("username", user["username"]);
		req.setParameter("password", user["password"]);
		req.send();

                // when login is finished, we open the application
                req.addListener("completed", function(e) {
                    this._initApplication(/*splash*/);
                }, this);
                
            }, this);
            
        },

        _initApplication: function(/*splash*/) {
            this._win = new eyeos.ui.Window(this, tr("Moodle"), 'index.php?extern=/images/moodle/theme/ico/moodle_16x16.gif').set({
                width: 640,
                height:480
            });

            var windowLayout = new qx.ui.layout.Grid();
            windowLayout.setColumnFlex(0, 1);
            windowLayout.setRowFlex(0, 0);
            windowLayout.setRowFlex(0, 1);

            this._win.setLayout(windowLayout);
            this._win.setAllowClose(true);
            this._win.setContentPadding(0);
            this._win.setShowStatusbar(true);
            this._win.setStatus("educME Project Version 0.6")
            this._win.maximize();
            this._win.open();


            this._iframe = new qx.ui.embed.Iframe( host() + "/moodle").set({
                decorator : null,
                opacity: 0.5
            });
            this._win.add(this._iframe, {row:0, column:0, flex: 1});

            this._iframe.addListener("load", function(e) {

                // restore iframe
                this._iframe.release();
                this._iframe.setOpacity(1);

                // destroy splash
                //splash.destroy();

                // control click event on iframe
                qx.event.Registration.addListener(this._iframe.getContentElement().getDomElement().contentDocument.body, "click", function(a) {
                    // if is a link, we must control the action
                    if (a.getTarget().tagName == "A" || a.getTarget().parentNode.tagName == "A") {
                        a.preventDefault();
                        var href = (a.getTarget().href != null) ? a.getTarget().href : a.getTarget().parentNode.href;
                        main(this._checknum, this._iframe, href);
                    }
                }, this);

            }, this);
        }   
    }
});